<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="任务编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskNo">
              <a-input v-model="model.taskNo" placeholder="请输入任务编号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskName">
              <a-input v-model="model.taskName" placeholder="请输入任务名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任务状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskStatus">
              <j-dict-select-tag type="list" v-model="model.taskStatus" dictCode="task_status" placeholder="请选择任务状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskBegin">
              <j-date placeholder="请选择开始时间" v-model="model.taskBegin" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskEnd">
              <j-date placeholder="请选择结束时间" v-model="model.taskEnd" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="已巡检数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectedNum">
              <a-input-number v-model="model.inspectedNum" placeholder="请输入已巡检数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="总点位数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pointNum">
              <a-input-number v-model="model.pointNum" placeholder="请输入总点位数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="执行周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executeCycle">
              <j-dict-select-tag type="list" v-model="model.executeCycle" dictCode="execute_cycle" placeholder="请选择执行周期" />
            </a-form-model-item>
          </a-col>

<!--          <a-col :span="12">
            <a-form-model-item label="规则" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleId">
              <j-dict-select-tag type="list" v-model="model.ruleId" dictCode="ins_rule,rule_name,id" placeholder="请选择规则" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roleId">
              <j-dict-select-tag type="list" v-model="model.roleId" dictCode="sys_role,role_name,id" placeholder="请选择角色" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deptId">
              <j-select-depart v-model="model.deptId" :multi="true"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-select-user-by-dep v-model="model.userId" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="执行人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executor">
              <a-input v-model="model.executor" placeholder="请输入执行人" ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction,putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: "InsTaskModal",
    components: {
    },
    data () {
      return {
        title:"操作",
        width:1000,
        visible: false,
        model:{
            taskStatus:"1",
            taskName: '',
            inspectedNum:0,
            pointNum:0,
            taskNo: '',
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           taskNo: [
              { required: true, message: '请输入任务编号!'},
           ],
           taskStatus: [
              { required: true, message: '请输入任务状态!'},
           ],
           taskBegin: [
              { required: true, message: '请输入开始时间!'},
           ],
           taskEnd: [
              { required: true, message: '请输入结束时间!'},
           ],
           inspectedNum: [
              { required: true, message: '请输入已巡检数!'},
           ],
           pointNum: [
              { required: true, message: '请输入总点位数!'},
           ],
        },
        url: {
          add: "/inspect/insTask/add",
          edit: "/inspect/insTask/edit",
          taskNo: ruleBaseURL + 'shop_order_num'
        }

      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getTaskNo() {
        putAction(this.url.taskNo, this.model).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            this.model.taskNo = res.result
          }
        })
      },
      add () {
        this.getTaskNo();
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>
